package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageBox;
import com.moleq.models.CouponSetting;
import com.moleq.models.GlobalInfo;

public class CouponSettingService {
	private DatabaseHelper DBHelper;
	private String errorString;
	private Context mContext;

	public CouponSettingService() {

	}

	public CouponSettingService(Context context) {
//		this.DBHelper = new DatabaseHelper(context);
		mContext = context;
		this.DBHelper = new DatabaseHelper(GlobalInfo.getInstance().ShareContext);
	}
	
	public List<CouponSetting> FindAll(){
		List<CouponSetting> obj = new ArrayList<CouponSetting>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from t_coupon_setting",null);
//		Cursor cursor = DBHelper.rawQuery("select * from t_coupon_setting",null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		while(cursor.moveToNext()){
			int cs_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.CouponSetting.CS_NO));
			String cs_desc  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.CouponSetting.CS_DESC));
			double cs_value  = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.CouponSetting.CS_VALUE));
			int cs_limit_qty = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.CouponSetting.CS_LIMIT_QTY));
			double cs_min_purchase = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.CouponSetting.CS_MIN_PURCHASE));

			CouponSetting cs = new CouponSetting(cs_no,cs_desc,cs_value,cs_limit_qty,cs_min_purchase);
			cs.setType("C");
			obj.add(cs);
		}
		cursor.close();
		return obj;
	}
	
	public CouponSetting Find(int no){
		CouponSetting obj = null;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = DBHelper.rawQuery("select * from t_coupon_setting where cs_no = "+no,null);
//		Cursor cursor = DBHelper.rawQuery("select * from t_coupon_setting where cs_no = "+no,null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return null;
		}
		while(cursor.moveToNext()){
			int cs_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.CouponSetting.CS_NO));
			String cs_desc  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.CouponSetting.CS_DESC));
			double cs_value  = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.CouponSetting.CS_VALUE));
			int cs_limit_qty = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.CouponSetting.CS_LIMIT_QTY));
			double cs_min_purchase = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.CouponSetting.CS_MIN_PURCHASE));
			
			obj = new CouponSetting(cs_no,cs_desc,cs_value,cs_limit_qty,cs_min_purchase);
		}
		cursor.close();
		return obj;
	}
	
	public boolean updateSetCouponNo(int coupon_no){
		boolean check = true;
		errorString = DBHelper.execSQL("UPDATE "+ DatabaseSchema.CouponSetting.SCHEMA_NAME +" set cs_no = cs_no - 1 where cs_no > " + coupon_no);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean insertCouponSetting(CouponSetting obj){
		boolean check = true;
		String sql,desc;
		double value,qty,min;
		desc = obj.getCs_desc();
		value = obj.getCs_value();
		qty = obj.getCs_limit_qty();
		min = obj.getCs_min_purchase();
		
		sql = "INSERT INTO "+ DatabaseSchema.CouponSetting.SCHEMA_NAME +" values";
		sql += "(null,'"+ desc +"',"+ value +","+ qty +","+ min +"," +
				"strftime('%Y%m%d%H%M%S','now'),strftime('%Y%m%d%H%M%S','now'))";
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean updateCouponSetting(CouponSetting obj){
		boolean check = true;
		String sql,desc;
		double value,qty,min;
		int no = obj.getCs_no();
		desc = obj.getCs_desc();
		value = obj.getCs_value();
		qty = obj.getCs_limit_qty();
		min = obj.getCs_min_purchase();
		
		sql = "UPDATE "+ DatabaseSchema.CouponSetting.SCHEMA_NAME +" set ";
		sql += "cs_desc = '"+ desc +"',cs_value = "+ value +",cs_limit_qty = "+ qty +"," +
				"cs_min_purchase = "+ min +",update_datetime = strftime('%Y%m%d%H%M%S','now')" +
				" where cs_no = "+ no;
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean deleteCouponSetting(int no){
		boolean check = true;
		String sql = "delete from "+ DatabaseSchema.CouponSetting.SCHEMA_NAME +" where cs_no = " + no;
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
}
